using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace ApplicationCore
{
    public class ProcessingInfo
    {
        //private EnzymeProcessingDataSet.BatchesRow batchOld;
        //private EnzymeProcessingDataSet.EnzymeCodesRow enzymeOld;
        public EnzymeView _enzyme;
        public BatchView _batch;
        private BatchLayout batLayout;


        public BatchLayout BatLayout
        {
            get { return batLayout; }
            set { batLayout = value; }
        }

        //public EnzymeProcessingDataSet.EnzymeCodesRow EnzymeOld
        //{
        //    get { return enzymeOld; }
        //    set { enzymeOld = value; }
        //}

        //public EnzymeProcessingDataSet.BatchesRow BatchOld
        //{
        //    get { return batchOld; }
        //    set { batchOld = value; }
        //}

        public EnzymeView Enzyme
        {
            get { return _enzyme; }
            set
            {
                _enzyme = value;
                _actViewValid = false;
            }
        }

        public BatchView Batch
        {
            get { return _batch; }
            set
            {

                _batch = value;
                _matViewValid = false;
                _actViewValid = false;
            }
        }

        public string BatchName
        {
            get
            {
                if (_batch != null)
                    return _batch.Name;
                else
                    return "--";
            }
        }

        public int BatchNumber
        {
            get
            {
                if (_batch != null)
                    return _batch.Number;
                else
                    return -1;
            }
        }

        public DateTime BatchDate
        {
            get
            {
                if (_batch != null)
                    return _batch.Date;
                else
                    return DateTime.MinValue;
            }
        }

        public string Type
        {
            get
            {
                if (this._enzyme != null)
                    return _enzyme.Type;
                else
                    return "--";
            }

        }

        public string LayoutType
        {
            get
            {
                if (this._batch != null)
                    return _batch.LayoutType;
                else
                    return "--";
            }
        }

        public string Protocol
        {
            get
            {
                if (this._batch != null)
                    return _batch.ProtocolName;
                else
                    return "--";
            }
        }

        public string AnalyteName
        {
            get
            {
                if (this._enzyme != null)
                    return _enzyme.Analyte;
                else
                    return "--";
            }

        }

        public string EnzymeCode
        {
            get
            {
                if (this._enzyme != null)
                    return _enzyme.Code;
                else
                    return "--";
            }

        }

        public double SloapRatio
        {
            get
            {
                if (this._enzyme != null)
                    return _enzyme.SlopeRatio;
                else
                    return 0;
            }
        }

        public string BatchDisplay
        {
            get
            {
                if (_batch != null)
                    return _batch.FullName;
                else
                    return "--";
            }
        }

        public string MaterialName
        {
            get
            {
                if (Material != null)
                {
                    return Material.Name;
                }
                return "--";
            }
        }


        private bool _matViewValid = false;
        private MaterialView _matView;
        public MaterialView Material
        {
            get
            {
                if (_matViewValid)
                    return _matView;
                if (batLayout.Layout != null)
                {
                    FreshWeightView frow;
                    MaterialView matRow;
                    for (int i = 0; i < batLayout.LayoutRowCount; i++)
                    {
                        for (int j = 0; j < batLayout.LayoutColumnCount; j++)
                        {
                            if (batLayout.IsControl(batLayout.Layout[i, j], out frow, out _matView))
                            {
                                _matViewValid = _matView != null;
                                return _matView;

                            }
                        }
                    }
                }
                return null;
            }
        }

        private bool _actViewValid = false;
        private ActivityView _actView;
        public double Activation
        {
            get
            {
                if (_actViewValid && _actView.Value.HasValue)
                    return _actView.Value.Value;
                if (_actViewValid && !_actView.Value.HasValue)
                    return 0f;

                FreshWeightView frow;
                MaterialView matRow;
                if (batLayout.Layout != null)
                {
                    for (int i = 0; i < batLayout.LayoutRowCount; i++)
                    {
                        for (int j = 0; j < batLayout.LayoutColumnCount; j++)
                        {
                            if (batLayout.IsControl(batLayout.Layout[i, j], out frow, out matRow))
                            {

                                ActivityView av = DataContext().ActivityViews.FirstOrDefault(x => x.Analyte == AnalyteName && x.MaterialUId == matRow.UId && x.ProtocolUId == _batch.ProtocolUId);
                                if (av != null && av.Value.HasValue)
                                {
                                    _actViewValid = true;
                                    _actView = av;
                                    return av.Value.Value;
                                }
                            }
                        }
                    }
                }
                return 0f;
            }
        }

        public Func<EnzymeDBDataContext> DataContext { get; set; }

    }
}
